package com.qh.pet.service.impl;

import com.qh.basic.service.IBaiduAiAuditService;
import com.qh.basic.util.AjaxResult;
import com.qh.org.domain.Department;
import com.qh.pet.domain.Pet;
import com.qh.pet.domain.PetDetail;
import com.qh.pet.domain.PetOnlineAuditLog;
import com.qh.pet.domain.PetType;
import com.qh.pet.mapper.PetDetailMapper;
import com.qh.pet.mapper.PetMapper;
import com.qh.pet.mapper.PetOnlineAuditLogMapper;
import com.qh.pet.mapper.PetTypeMapper;
import com.qh.pet.service.IPetOnlineAuditLogService;
import com.qh.pet.service.IPetService;
import com.qh.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetTypeMapper petTypeMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    /**
     * 类型树
     * @return
     */
    @Override
    public List<PetType> getPetTree() {
        //1.全部查出来，存到map中--内存
        List<PetType> petTypes = petTypeMapper.loadAll();
        HashMap<Long, PetType> map = new HashMap<>();
        for(PetType petType:petTypes){
            map.put(petType.getId(),petType);
        }
        //2.遍历所有部门如果没有parent_id直接装到树里面
        ArrayList<PetType> petTypeTree = new ArrayList<>();
        for(PetType petType:petTypes){
            if (petType.getParentId() == null){
                //没有上级的，直接加到树里
                petTypeTree.add(petType);
            }else {//有上级的
                //1.先查到父id
                Long parentId = petType.getParentId();
                //2.通过parentId查到自己的父亲
                PetType petTypeParent = map.get(parentId);
                //3.在装到树里
                petTypeParent.getChildren().add(petType);
            }
        }

        return petTypeTree;
    }

    @Override
    public void save(Pet pet) {
        //先添加pet
        super.save(pet);
        //再添加petDetail
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    @Override
    public void del(Long id) {
        super.del(id);
        petDetailMapper.delDetailByPetId(id);
    }

    @Override
    public void update(Pet pet) {
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updateDetailByPetId(petDetail);
        }
        super.update(pet);
    }

    @Override
    public void patchDel(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    /**
     * 上架
     * @param ids
     */
    @Transactional
    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            //先查出来
            Pet pet = petMapper.loadOne(id);
            //使用百度aiutil审核
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            log.setPetId(id);
            if (boo1 && boo2){//审核通过
                //改状态
                petMapper.onsale(id,new Date());
                //添加审核日志
                log.setState(1);
                log.setNote("审核成功");
                petOnlineAuditLogMapper.save(log);
            }else {
                log.setState(0);//审核失败
                log.setNote("审核失败");
                petOnlineAuditLogMapper.save(log);
                return AjaxResult.me().setSuccess(false).setMsg("上架失败");
            }
        }
        return new AjaxResult();
    }
    /**
     * 下架
     * @param ids
     */
    @Transactional
    @Override
    public AjaxResult offsale(List<Long> ids) {
        petMapper.offsale(ids,new Date());
        return new AjaxResult();
    }


}
